$(function () {
    var bill_datagrid = $("#bill_datagrid");
    var bill_dialog = $("#bill_dialog");
    var bill_form = $("#bill_form");

    bill_datagrid.datagrid({
        url: '/orderBill/query.do',
        fit: true,
        fitColumns: true,
        columns: [
            [
                {field: 'sn', title: '订单编号', width: 100},
                {field: 'totalNumber', title: '订单总数量', width: 100},
                {field: 'totalAmount', title: '总金额', width: 100},
                {field: 'vdate', title: '添加时间', width: 100},
                {field: 'auditTime', title: '审核时间', width: 100},
                {
                    field: 'inputUser', title: '录入人', width: 100, formatter: function (value) {
                    console.log(value);
                    return value ? value.realname : "";
                }
                },
                {
                    field: 'auditor', title: '审核人', width: 100, formatter: function (value) {
                    return value ? value.realname : "";
                }
                },
                {
                    field: 'supplier', title: '供应商', width: 100, formatter: function (value) {
                    return value ? value.name : "";
                }
                },
                {
                    field: 'depot', title: '仓库', width: 100, formatter: function (value) {
                    return value ? value.name : ''
                }
                },
                {
                    field: 'state', title: '状态', width: 100, formatter: function (value) {
                    return value ? '<font color="red">未审核</font>' : '已审核';
                }
                }
            ]
        ],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: '#tb',
        onOpen: function () {
            $.get('/orderBill/getSn.do', function (data) {
                $("#mySn").textbox('setValue', data)
            })
        },
        
    })

    bill_dialog.dialog({
        title: '新增采购订单',
        width: 700,
        height: 480,
        buttons: '#form_btn',
        closed: true,
        onClose: function () {
            bill_form.form('clear')
        }
    })

    var methodObj = {
        add: function () {
            bill_dialog.dialog('open');
        },
        edit: function () {
            var row = bill_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            bill_form.form('load', row);
            bill_dialog.dialog('open');
            bill_dialog.dialog('setTitle', '编辑订单');
        },
        changeState: function () {
            var row = bill_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            alert("111"),
            $.messager.confirm("确认对话框", "你确定要执行操作吗?", function (r) {
                if (r) {
                    $.get('/orderBill/changeState.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, 'info', function () {
                                methodObj['cancel']();
                                methodObj['reload']();
                            })
                        } else {
                            $.messager.alert("温馨提示", data.msg, 'warning')
                        }
                    })
                }
            })
        },
        reload: function () {
            bill_datagrid.datagrid('reload');
        },
        save: function () {
            bill_form.form("submit", {
                url: '/orderBill/saveOrUpdate.do',
                onSubmit:function (param) {
                  param.items.productName=product;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            methodObj['cancel']();
                            methodObj['reload']();
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'warning')
                    }
                }
            })
        },
        cancel: function () {
            bill_dialog.dialog('close')
        },


    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
$(function () {
    $('#dd').dialog({
        title: '商品明细',
        width: 800,
        height: 500,
        closed: true,
    });
    $("#add_datagrid").datagrid({
        url: '/product/query.do',
        fit: true,
        fitColumns: true,
        columns: [
            [
                {field: 'productName', title: '商品名称', width: 100},
                {field: 'productSn', title: '商品编号', width: 100},
                {field: 'costPrice', title: '商品成本价', width: 100},
                {
                    field: 'productSort_one', title: '一级分类', width: 100, formatter: function (value) {
                    return value ? value.name : "";
                }
                },
                {
                    field: 'productSort_two', title: '二级分类', width: 100, formatter: function (value) {
                    return value ? value.name : "";
                }
                },
                {
                    field: 'operate', title: '操作', width: 100,
                    formatter: function (value, row, index) {
                        var str = '<a href="#" name= "opera" class="easyui-linkbutton" onclick="add_seve()"></a>';
                        return str
                    },
                }
            ]
        ],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        onLoadSuccess: function (data) {
            $("a[name='opera']").linkbutton({text: '下订单', iconCls: 'icon-add'});
        }

    });
    $('#add_linkbutton').bind('click', function () {
        $("#dd").dialog('open');
    });

})
function add_cancel() {
    $("#dd").dialog('close');
}
function add_seve() {
    var row = $("#add_datagrid").datagrid('getSelected');
    if (!row) {
        return;
    }
    $("#productName").textbox('setValue', row.name)
    $("#productSn").textbox('setValue', row.sn)
    $("#costPrice").textbox('setValue', row.costPrice)
    add_cancel();
}


$(function () {
        $("#number,#costPrice").textbox({
            onChange: function () {
                var costPrice = $("#costPrice").textbox('getValue')
                var number = $("#number").textbox('getValue')
                $("#total").textbox('setValue', costPrice * number)
            }
        });
        
    }
)
function add_button() {
    // 1. 获取到表格中的第一行,并克隆
    var tr = $("#edit_table_body tr:first").clone(true);
    // 2. 清空行中的数据

    // 3. 追加到表格中
    tr.appendTo("#edit_table_body");
}

$(function () {
    $("#edit_table_body").on("click", ".removeItem", function () {
        var tr = $(this).closest("tr");
        console.log(tr);
        // 1. 获取所有的tr,判断tr数组是否大于1
        if ($("#edit_table_body tr").size() > 1) {
            // 2. 如果数量大于1,直接删除当前行
            tr.remove();
        }
    })
})


